package com.hok.misc.file.separate;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.Map;

public class FileMerge {
	public static void main(String[] args) {
		File file1 = new File("C:\\Program Files\\Tencent\\QQ\\Users\\182188423\\FileRecv\\name.txt");
		File file2 = new File("C:\\Program Files\\Tencent\\QQ\\Users\\182188423\\FileRecv\\fee.TXT");
		File result = new File("C:\\Program Files\\Tencent\\QQ\\Users\\182188423\\FileRecv\\result.txt");
		
		Map<String, String> nameMap = new HashMap<String, String>();
		try {
			LineNumberReader reader1 = new LineNumberReader(new BufferedReader(new FileReader(file1)));
			String line = "";
			long start = System.currentTimeMillis();
			while((line = reader1.readLine( )) != null){
				String[] numName = line.split(",");
				if(numName.length >= 2) {
					nameMap.put(numName[0], numName[1]);
				} else {
					nameMap.put(numName[0], "");
				}
			}
			System.out.println((System.currentTimeMillis() - start)/1000);
			reader1.close();
			FileWriter fw = new FileWriter(result, true);
			BufferedWriter bw = new BufferedWriter(fw);
			  
			LineNumberReader reader2 = new LineNumberReader(new BufferedReader(new FileReader(file2)));
			long start2 = System.currentTimeMillis();
			while((line = reader2.readLine( )) != null){
				//String area = line.substring(0, 3);
				String num = line.substring(3, 15);
				//String brand = line.substring(15, 21);
				//String fee = line.substring(21, line.length());
				bw.write(line + nameMap.get(num.trim()));
				bw.newLine();
			}
			System.out.println((System.currentTimeMillis() - start2)/1000);
			bw.flush(); //����ݸ������ļ�
			bw.close();
			fw.close();
			  
			reader2.close();
			//
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String getSpace(int i) {
		if(i == 0) {
			return " ";
		}
		StringBuilder space = new StringBuilder();
		for (int j = 0; j < i; j++) {
			space.append(" ");
		}
		return space.toString();
	}
}
